package com.kgc.springboot.aviation.service.lvwenjia.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.mapper.ClauseMapper;
import com.kgc.springboot.aviation.pojo.Clause;
import com.kgc.springboot.aviation.pojo.ClauseExample;
import com.kgc.springboot.aviation.service.lvwenjia.ClauseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-12-29 15:24
 */
@Service
public class ClauseServiceImpl implements ClauseService {
    @Resource
    ClauseMapper clauseMapper;

    /**
     *
     * @param lawsId
     * @param clauseId
     * @param keyword
     * @param pageIndex
     * @return 分页
     */
    @Override
    public PageInfo<Clause> selectClause(Integer lawsId, String clauseId, String keyword, int pageIndex) {
        PageHelper.startPage(pageIndex,5);
        ClauseExample clauseExample=new ClauseExample();
        ClauseExample.Criteria criteria=clauseExample.createCriteria();
        if(lawsId!=0){
            criteria.andLawsIdEqualTo(lawsId);
            System.out.println("进入lawsId");
        }
        if(!clauseId.equals("")){
            criteria.andClauseIdEqualTo(clauseId);
            System.out.println("clauseId");
        }
        if(!keyword.equals("")){
            criteria.andKeywordEqualTo(keyword);
            System.out.println("keyword");
        }
        System.out.println(clauseExample);
        System.out.println(lawsId+"\t"+clauseId+"\t"+keyword+"\t"+pageIndex);
        List<Clause> list=clauseMapper.selectByExample(clauseExample);
        PageInfo<Clause> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int delClauseById(Integer id) {
        int i = clauseMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 添加
     * @param clause
     * @return
     */
    @Override
    public int insertClause(Clause clause) {
        int i = clauseMapper.insertSelective(clause);
        return i;
    }

    /**
     * 修改
     * @param clause
     * @return
     */
    @Override
    public int updateClause(Clause clause) {
        int i = clauseMapper.updateByPrimaryKeySelective(clause);
        return i;
    }

    @Override
    public Clause selectClauseById(Integer id) {
        Clause clause = clauseMapper.selectByPrimaryKey(id);
        return clause;
    }

    @Override
    public Clause getAll(Integer catalogId) {
        ClauseExample example=new ClauseExample();
        if (catalogId!=null && catalogId>0){
            example.createCriteria().andCatalogIdEqualTo(catalogId);
            List<Clause> list = clauseMapper.selectByExample(example);
            if (list!=null&&list.size()>0){
                return list.get(0);
            }
        }
        return null;
    }
}
